<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use Cookie;
use DB;
class LoginController extends Controller
{
   //登录页面
    public function getIndex()
    {
        return view('admin.login.index');
    }

    //登录处理
    public function postLogaction(Request $requests)
    {
        //ajax接收
        $data = $requests -> except('_token');
        $code = session('code');
        $str_code = strtolower($code);
        $str = strtolower($data['verify']);
        if($str_code==$str){
             //判断用户是否存在
            $row = DB::table('sa_admin as ad')
                -> leftjoin('sa_agent as ag','ag.id','=','ad.agent_id')
                -> select('ad.*','ag.agent_name')
                -> where(['ad.phone'=>$data['account']])
                -> orwhere(['ad.account'=>$data['account']])
                -> first();
            if($row){
                if($row['status'] == 1) {
                    if (Hash::check($data['password'], $row['password'])) {
                        // session(['agentId'=>$row['id']]);
                        Cookie::queue('agentId', $row['agent_id'], 0);
                        Cookie::queue('adminId', $row['id'], 0);
                        Cookie::queue('user', $row, 0);
                        // dd(Cookie::get('user')['id']);
                        $arr['status'] = '1';
                        $arr['msg'] = '登录成功';
                    } else {
                        $arr['status'] = '-2';
                        $arr['msg'] = '请输入正确的密码';
                    }
                }else{
                    $arr['status'] = '-4';
                    $arr['msg'] = '您的账号已被禁用,请联系客服解禁';
                }
            }else{      
                $arr['status'] = '-1';
                $arr['msg'] = '您输入的账号不存在';
            }
        }else{
           $arr['status'] = '-3';
            $arr['msg'] = '您输入的验证码错误';
        }
    echo json_encode($arr);
    }
    // 注销用户
    public function getExit()
    {
        // $cookie = Cookie::forget('agentId');
        Cookie::queue('agentId', null , -1); // 销毁
        Cookie::queue('adminId', null , -1); // 销毁

        Cookie::queue('user',null,-1);
        // return Redirect::route('admin')->withCookie($cookie);
        return redirect('/');
    }

    //忘记密码
    public function getLost(){
        return view('admin.login.lost');
    }

    //忘记密码处理页面
    public function postLost(Request $request){
        //获取ajax值
        $data = $request -> except('_token');
        //查询代理商表
        $row = DB::table('sa_admin') -> where($data) -> first();
        if($row){
            $arr['status'] = '1';
            $arr['msg'] = '用户名匹配成功';
            $arr['agentId'] = base64_encode($row['id']);
        }else{
            $arr['status'] = '-1';
            $arr['msg'] = '请输入正确的账号和手机号';
        }
        echo json_encode($arr);
    }

    //修改密码
    public function getChange(Request $request){
        $agentId = base64_decode($request -> get('ag'));
        return view('admin.login.change',['agentId'=>$agentId]);
    }

    //修改密码处理页面
    public function postChange(Request $request){
        //获取ajax值
        $id = $request -> input('agentId');
        $pass = $request -> input('pass');
        $password = Hash::make(trim($pass));
        //查询代理商表
        $row = DB::table('sa_admin') -> where(compact('id')) -> update(compact('password'));
        if($row){
            $arr['status'] = '1';
            $arr['msg'] = '密码修改成功';
        }else{
            $arr['status'] = '-1';
            $arr['msg'] = '密码修改失败';
        }
        echo json_encode($arr);
    }

    //布撤防页面
    public function getDisarm(Request $request){
        //查询设备布撤防状态
        $DevEui = $request -> input('eui');
        $res = DB::table('sa_room_detector') -> select('rd_sd_eui','rd_status') -> where('rd_sd_eui','=',$DevEui) -> first();
        return view('admin.login.disarm',['res'=>$res]);
    }

    //布防撤防处理
    public function postDisarm(Request $request){
        $DevEui = $request -> input('eui');
        $status = $request -> input('status');
        if($status == 0){
            $res = DB::table('sa_room_detector') -> where('rd_sd_eui','=',$DevEui) -> update(['rd_status'=>0]);
           if($res){
               return 0;
           }
        }else{
            $res = DB::table('sa_room_detector') -> where('rd_sd_eui','=',$DevEui) -> update(['rd_status'=>1]);
            if($res){
                return 1;
            }
        }
    }
}
